package com.vitabuybuy.tv.ui.mine.talent.live

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityChooseNoticeBinding
import com.vitabuybuy.tv.ui.mine.talent.live.adapter.ChooseNoticeAdapter
import com.vitabuybuy.tv.ui.mine.talent.live.viewmodel.StartLiveViewModel

class ChooseNoticeActivity : BaseActivity<StartLiveViewModel, ActivityChooseNoticeBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_choose_notice

    private val mAdapter by lazy { ChooseNoticeAdapter() }
    private var pageNumber = 1

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.choose_notice))
        mBinding.viewModel = viewModel
        mTvRight.apply {
            visibility = View.VISIBLE
            text = getString(R.string.confirm)
        }
        mBinding.listview.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun observeData() {
        viewModel.getNoticeList(pageNumber)
        viewModel.noticeList.observe(this, Observer {
            when (pageNumber) {
                1 -> mAdapter.setNewData(it)
                else -> mAdapter.addData(it)
            }
            when (mAdapter.mData.size) {
                0 -> viewModel.statusView.set(LoadingType.SHOW_EMPTY)
                else -> viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
            mBinding.refresh.onLoad(it.size)
        })
    }

    override fun eventHandling() {
        mTvRight.setOnClickListener {
            val noticeList = ArrayList<HashMap<String, Int>>()
            for (bean in mAdapter.mData) {
                if (bean.isChecked) {
                    val idMap = HashMap<String, Int>().apply {
                        this["id"] = bean.id
                    }
                    noticeList.add(idMap)
                }
            }
            if (noticeList.isEmpty()) return@setOnClickListener
            val intent = Intent()
            intent.putExtras(bundleOf(Pair("noticeList", noticeList)))
            finishResult(intent)
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            mAdapter.mData[position].isChecked = !mAdapter.mData[position].isChecked
            mAdapter.notifyItemChanged(position)
        }
        mBinding.refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getNoticeList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getNoticeList(pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getNoticeList(pageNumber)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startForResult(null, 0x12, ChooseNoticeActivity::class.java)
        }
    }
}
